package com.venue.hjt.venueclass.adapter;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.venue.hjt.venueclass.R;
import com.venue.hjt.venueclass.db.CardDao;
import com.venue.hjt.venueclass.db.VenueEvaluate;
import com.venue.hjt.venueclass.view.StarBar;

import java.util.List;

/**
 * Created by xqx on 2017/10/25.
 * 照片墙
 */

public class VenuePAdapter extends BaseAdapter {
    private Context mContext;
    private List<VenueEvaluate> list;
    private boolean isP=true;

    public VenuePAdapter(Context mContext, List<VenueEvaluate> list) {
        this.mContext = mContext;
        this.list = list;
    }
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHander viewHander;
        if (convertView==null){
            viewHander=new ViewHander();
            convertView=View.inflate(mContext, R.layout.p_item,null);
            viewHander.starBar= convertView.findViewById(R.id.starBar);
            viewHander.tv_p_score= convertView.findViewById(R.id.tv_p_score);
            viewHander.tv_p_content= convertView.findViewById(R.id.tv_p_content);
            viewHander.tv_content= convertView.findViewById(R.id.tv_content);
            viewHander.tv_title= convertView.findViewById(R.id.tv_title);
            convertView.setTag(viewHander);

        }else{
            viewHander= (ViewHander) convertView.getTag();
        }

        final VenueEvaluate evaluate = list.get(position);
        if (isP){
            viewHander.starBar.setStarMark(Integer.parseInt(evaluate.getScore()));
        }
        viewHander.tv_content.setText(evaluate.getRemark());
        viewHander.tv_title.setText(evaluate.getTitle());
        switch (Integer.parseInt(evaluate.getScore())){
            case 1:
                isP=false;
                viewHander.tv_p_content.setText(evaluate.getStar1());
                break;
            case 2:
                isP=false;
                viewHander.tv_p_content.setText(evaluate.getStar2());
                break;
            case 3:
                isP=false;
                viewHander.tv_p_content.setText(evaluate.getStar3());
                break;
            case 4:
                isP=false;
                viewHander.tv_p_content.setText(evaluate.getStar4());
                break;
            case 5:
                isP=false;
                viewHander.tv_p_content.setText(evaluate.getStar5());
                break;

        }
        viewHander.tv_p_score.setText((Integer.parseInt(evaluate.getScore())*2)+".0");
        viewHander.starBar.setOnStarChangeListener(new StarBar.OnStarChangeListener() {
            @Override
            public void onStarChange(float mark) {
                switch ((int) mark){
                    case 1:
                        viewHander.tv_p_score.setText("2.0");
                        viewHander.tv_p_content.setText(evaluate.getStar1());
                        evaluate.setScore("1");
                        CardDao.updateEvaluate(evaluate);
                        Log.e("ATG","1  "+mark);
                        break;
                    case 2:
                        viewHander.tv_p_score.setText("4.0");
                        viewHander.tv_p_content.setText(evaluate.getStar2());
                        evaluate.setScore("2");
                        CardDao.updateEvaluate(evaluate);
                        Log.e("ATG","2  "+mark);
                        break;
                    case 3:
                        viewHander.tv_p_score.setText("6.0");
                        viewHander.tv_p_content.setText(evaluate.getStar3());
                        evaluate.setScore("3");
                        CardDao.updateEvaluate(evaluate);
                        Log.e("ATG","3  "+mark);
                        break;
                    case 4:
                        viewHander.tv_p_score.setText("8.0");
                        viewHander.tv_p_content.setText(evaluate.getStar4());
                        evaluate.setScore("4");
                        CardDao.updateEvaluate(evaluate);
                        Log.e("ATG","4  "+mark);
                        break;
                    case 5:
                        viewHander.tv_p_score.setText("10");
                        viewHander.tv_p_content.setText(evaluate.getStar5());
                        evaluate.setScore("5");
                        CardDao.updateEvaluate(evaluate);
                        Log.e("ATG","5  "+mark);
                        break;
                }

            }
        });
        return convertView;
    }
    class ViewHander{
        private TextView tv_p_score,tv_p_content,tv_content,tv_title;
        StarBar starBar;
    }

    @Override
    public boolean isEnabled(int position) {
        return false;
    }
}
